/**  
 * Copyright © 2014公司名字. All rights reserved.
 *
 * @Title: ExcelServiceImpl.java
 * @Prject: web
 * @Package: yang.service.impl
 * @Description: TODO
 * @author: Administrator  
 * @date: 2014年7月12日 上午11:54:08
 * @version: V1.0  
 */
package yang.service.impl;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import yang.dao.BaseDaoI;
import yang.model.FinanceInfo;
import yang.service.ExcelServiceI;

/**
 * @ClassName: ExcelServiceImpl
 * @Description: 输出Excel
 * @author: Administrator
 * @date: 2014年7月12日 上午11:54:08
 */
@Service("excelService")
public class ExcelServiceImpl implements ExcelServiceI {
	private  BaseDaoI<FinanceInfo> financeDao;
	@Override
	public  void reprotExcel(HttpServletResponse response) throws IOException {
		List<FinanceInfo> pageDataList = null;
		String fileName = "医院门诊业绩表";
		pageDataList = getDataListByDeptMothly();
		
		OutputStream out = response.getOutputStream();
		response.reset();
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment; fileName=" + new String((fileName+".xls").getBytes(), "iso8859-1"));	
		try {
			WritableWorkbook wbook = Workbook.createWorkbook(out); // 建立excel文件
			WritableSheet wsheet = wbook.createSheet("导出数据", 0); // sheet名称
			
			WritableCellFormat cellFormatNumber = new WritableCellFormat();
			cellFormatNumber.setAlignment(Alignment.RIGHT);

			WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,
					WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK); // 定义格式、字体、粗体、斜体、下划线、颜色
			
			WritableCellFormat wcf = new WritableCellFormat(wf); // title单元格定义
			WritableCellFormat wcfc = new WritableCellFormat(); // 一般单元格定义
			WritableCellFormat wcfe = new WritableCellFormat(); // 一般单元格定义
			wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
			wcfc.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式

			wcf.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			wcfc.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			wcfe.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);

			wsheet.setColumnView(0, 20);// 设置列宽
			wsheet.setColumnView(1, 10);
			wsheet.setColumnView(2, 20);

			int rowIndex = 0;
			int columnIndex = 0;
			if (null != pageDataList) {
				// rowIndex++;
				columnIndex = 0;
				wsheet.setRowView(rowIndex, 500);// 设置标题行高
				wsheet.addCell(new Label(columnIndex++, rowIndex, fileName, wcf));
				wsheet.mergeCells(0, rowIndex, 5, rowIndex);// 合并标题所占单元格
				rowIndex++;
				columnIndex = 0;
				wsheet.setRowView(rowIndex, 380);// 设置项目名行高
				wsheet.addCell(new Label(columnIndex++, rowIndex, "时间", wcf));
				wsheet.addCell(new Label(columnIndex++, rowIndex, "金额", wcf));
				wsheet.addCell(new Label(columnIndex++, rowIndex, "类型", wcf));
				// 开始行循环
				for (FinanceInfo array : pageDataList) { // 循环列
					rowIndex++;
					columnIndex = 0;
					wsheet.addCell(new Label(columnIndex++, rowIndex, array.getFinanceTime().toString(),
							wcfe));
					wsheet.addCell(new Label(columnIndex++, rowIndex, array.getFinanceMoney().toString(),
							wcfe));
					wsheet.addCell(new Label(columnIndex++, rowIndex, array.getFinanceType(),
							wcfe));					
				}

				rowIndex++;
				columnIndex = 0;
			}

			wbook.write();
			if (wbook != null) {
				wbook.close();
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public  List<FinanceInfo> getDataListByDeptMothly() {
		List<FinanceInfo> list =financeDao.find("from FinanceInfo");
		
		return list;
	}
	//-----get set-------
	public BaseDaoI<FinanceInfo> getFinanceDao() {
		return financeDao;
	}
	@Autowired
	public void setFinanceDao(BaseDaoI<FinanceInfo> financeDao) {
		this.financeDao = financeDao;
	}
	
}
